#include "addfriendreceiver.h"
#include "ui_addfriendreceiver.h"
#include "friendlist.h"
addfriendreceiver::addfriendreceiver(QWidget *parent,user userSelf,QVariantMap pullChatMap,QWebSocket* socketi) :
    QDialog(parent),
    ui(new Ui::addfriendreceiver)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
    this->socket=socketi;
    //连接到server
    //获取信息
    this->pullMap=pullChatMap;//添加聊天信息处理
    //websocket传输代码
    //获取信息
    QMap senderMap = pullMap.value("sender").toMap();
    QString gender = senderMap.value("gender").toString();
    qDebug()<<gender;
    QString id = senderMap.value("id").toString();
    QString brief=pullMap.value("brief").toString();
    ui->senderid->append(id);//对话框中添加id
    //对话框中添加gender
    if(gender=="1")
        ui->sendergender->append("男");
    else if(gender=="0")
        ui->sendergender->append("女");
    else
        ui->sendergender->append("未知");
    ui->senderbrief->append(brief);//对话框中添加brief
    connect(ui->accept,&QPushButton::clicked,this, &addfriendreceiver:: accept_clicked);//接受好友
    connect(ui->refuse,&QPushButton::clicked,this, &addfriendreceiver:: refuse_clicked);//拒绝好友

}
addfriendreceiver::~addfriendreceiver()
{
    delete ui;
}
void addfriendreceiver:: accept_clicked()
{
    int IsAcceptedCode=2;//2为接受
    UserBrief Self;//创建Self结构体来访问userSelf数据
    Self = {
        userSelf.getUserId(),//id
        userSelf.getUserName(),//username
        "",//photo
        userSelf.getUserGender()//gender
    };
    QMap senderMap = pullMap.value("sender").toMap();//处理发送信息的sender对应的value
    QVariant username = senderMap.value("username");
    QVariant gender = senderMap.value("gender");
    QVariant id = senderMap.value("id");
    UserBrief TheOther;
    TheOther ={//收到请求的人
        id.toString(),
        username.toString(),
        "",
        gender.toString()
    };
    //sender信息
    QJsonObject senderValue{//发送到后端的请求者
        {"id", Self.id.toInt()},
        {"username", Self.username},
        {"photo", "0"},
        {"gender", Self.gender.toInt()}
    };
    QJsonObject receiverValue{//发送到后端的接受者
        {"id", TheOther.id.toInt()},
        {"username", TheOther.username},
        {"photo", "0"},
        {"gender", TheOther.gender.toInt()}
    };
    //发送的信息
    QJsonObject json{//发送到后端的json对象
        {"isAcceptedCode",IsAcceptedCode},
        {"self",senderValue},
        {"theOther",receiverValue},
        {"type", "add_friend_fail_or_success"}
    };
    QJsonDocument doc(json);//转化为doc文件
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    this->close();
    this->socket->sendTextMessage(data);//发送Json

}
void addfriendreceiver:: refuse_clicked()
{
    int IsAcceptedCode=1;//1为拒绝
    UserBrief Self;//创建Self结构体来访问userSelf数据
    Self = {
        userSelf.getUserId(),//id
        userSelf.getUserName(),//username
        "",//photo
        userSelf.getUserGender()//gender
    };
    QMap senderMap = pullMap.value("sender").toMap();//处理发送信息的sender对应的value
    QVariant username = senderMap.value("username");
    QVariant gender = senderMap.value("gender");
    QVariant id = senderMap.value("id");
    UserBrief TheOther;
    TheOther ={//收到请求的人
        id.toString(),
        username.toString(),
        "",
        gender.toString()
    };
    //sender信息
    QJsonObject senderValue{//发送到后端的请求者
        {"id", Self.id.toInt()},
        {"username", Self.username},
        {"photo", "0"},
        {"gender", Self.gender.toInt()}
    };
    QJsonObject receiverValue{//发送到后端的接受者
        {"id", TheOther.id.toInt()},
        {"username", TheOther.username},
        {"photo", "0"},
        {"gender", TheOther.gender.toInt()}
    };
    //发送的信息
    QJsonObject json{//发送到后端的json对象
        {"isAcceptedCode",IsAcceptedCode},
        {"self",senderValue},
        {"theOther",receiverValue},
        {"type", "add_friend_fail_or_success"}
    };
    QJsonDocument doc(json);//转化为doc文件
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    this->close();
    this->socket->sendTextMessage(data);//发送Json
}
